﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using Feifansoft.Core.Authentication;
using Feifansoft.Core.Common.Result;
using Feifansoft.Core.Common.Utils;
using FeifanSoft.Core.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Feifansoft.Core.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class LoginController : ControllerBase
    {
        private readonly IJwtService _iJwtService;

        public LoginController(IJwtService iJwtService)
        {
            _iJwtService = iJwtService;
        }

        /// <summary>
        /// 登录验证，获取token
        /// </summary>
        /// <param name="code">账号</param>
        /// <param name="pwd">默默</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetJwtToken(string code,string pwd)
        {
            try
            {
                // 读取数据库，验证



                var sysUser = new S_User
                {
                    S_UserID = "User001"
                };

                var claims = new[]
                {
                    new Claim("userid",Md5Helper.Md5Encrypt(sysUser.S_UserID))
                };

                var token = _iJwtService.GetJwtToken(claims);

                return new JsonResult(Result.Successed(token));
            }
            catch (Exception ex)
            {
                return new JsonResult(Result.Failed(ex.Message)); 
            }
        }
    }
}